using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Runtime.Serialization;
using TemplateWeb.DAO;
using TemplateWeb.Helpers;
using System.Web.Security;

namespace TemplateWeb.DTO
{
	/// <summary>
	/// Data transfer object which is associated with PhienDauGia table in database
	/// </summary>
	public class PhienDauGiaDTO
	{
		public int ID { get; set; }
		public int IDHinhThucDauGia { get; set; }
		public int IDSanPham { get; set; }
		public Guid IDNguoiDang { get; set; }
		public DateTime ThoiGianBatDau { get; set; }
		public DateTime ThoiGianKetThuc { get; set; }
		public long GiaKhoiDiem { get; set; }
		public long GiaToiThieu { get; set; }
		public long TienDatCoc { get; set; }
		public string KetQua { get; set; }
		public bool XuatHienOTrangChu { get; set; }
		public bool Duyet { get; set; }

        public SanPhamDTO SanPham { get; set; }
        public HinhThucDauGiaDTO HinhThucDauGia { get; set; }
        public NguoiDungDTO NguoiDung { get; set; }
        public List<NguoiChoiDauGiaDTO> NguoiChoiDauGia { get; set; }

		public static PhienDauGiaDTO Translate(DataRow row)
		{
			var dto = new PhienDauGiaDTO
			{
				ID = int.Parse(row["ID"].ToString()),
				IDHinhThucDauGia = int.Parse(row["IDHinhThucDauGia"].ToString()),
                IDSanPham = int.Parse(row["IDSanPham"].ToString()),
				IDNguoiDang = Guid.Parse(row["IDNguoiDang"].ToString()),
				ThoiGianBatDau = DateTime.Parse(row["ThoiGianBatDau"].ToString()),
				ThoiGianKetThuc = DateTime.Parse(row["ThoiGianKetThuc"].ToString()),
				KetQua = row["KetQua"].ToString(),
				XuatHienOTrangChu = bool.Parse(row["XuatHienOTrangChu"].ToString()),
				Duyet = bool.Parse(row["Duyet"].ToString()),
			};

            if (!string.Empty.Equals(row["GiaKhoiDiem"].ToString()))
            {
                dto.GiaKhoiDiem = long.Parse(row["GiaKhoiDiem"].ToString());
            }
            if (!string.Empty.Equals(row["GiaToiThieu"].ToString()))
            {
                dto.GiaToiThieu = long.Parse(row["GiaToiThieu"].ToString());
            }
            if (!string.Empty.Equals(row["TienDatCoc"].ToString()))
            {
                dto.TienDatCoc = long.Parse(row["TienDatCoc"].ToString());
            }

            var rpt = new SanPhamDAO(WebConfiguration.DbConnectionString);
            var objDS = rpt.SelectByID(dto.IDSanPham);

            if (objDS != null && objDS.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow datarow in objDS.Tables[0].Rows)
                {
                    // Add tat ca nguoi choi co tham gia phien dau gia vao list
                    dto.SanPham = new SanPhamDTO
                    {
                        ID = dto.IDSanPham,
                        IDDanhMucCon = int.Parse(row["IDDanhMucCon"].ToString()),
                        IDToChucCaNhan = int.Parse(row["IDToChucCaNhan"].ToString()),
                        TenSanPham = row["TenSanPham"].ToString(),
                        XuatXu = row["XuatXu"].ToString(),
                        MoTa = row["MoTa"].ToString(),
                        HinhAnh = row["HinhAnh"].ToString(),
                        SoLuongTon = int.Parse(row["SoLuongTon"].ToString())
                    };
                    dto.SanPham.DanhMucCon = new DanhMucConDTO
                    {
                        ID = dto.SanPham.IDDanhMucCon,
                        IDDanhMucChinh = int.Parse(row["IDDanhMucChinh"].ToString()),
                        TenDanhMucCon = row["TenDanhMucCon"].ToString()
                    };
                    dto.SanPham.DanhMucCon.DanhMucChinh = new DanhMucChinhDTO
                    {
                        ID = dto.SanPham.DanhMucCon.IDDanhMucChinh,
                        TenDanhMucChinh = row["TenDanhMucChinh"].ToString()
                    };
                }
            }

            var htdg = new HinhThucDauGiaDTO
            {
                ID = dto.IDHinhThucDauGia,
                TenHinhThucDauGia = row["TenHinhThucDauGia"].ToString(),
            };

            var nd = new NguoiDungDTO
            {
                ID = dto.IDNguoiDang,
                HoTen = row["HoTen"].ToString(),
                DiaChi = row["DiaChi"].ToString(),
                SoDienThoai = row["SoDienThoai"].ToString(),
                SoTaiKhoan = row["SoTaiKhoan"].ToString(),
            };
            var mUser = Membership.GetUser(nd.ID);
            nd.aspnet_Users = new BaseInfo(mUser.UserName, mUser.Email, Roles.GetRolesForUser(mUser.UserName)[0], mUser.CreationDate);


            dto.HinhThucDauGia = htdg;
            dto.NguoiDung = nd;
            dto.NguoiChoiDauGia = new List<NguoiChoiDauGiaDTO>();

            // Lay danh sach nguoi dung dau gia
            var rpt2 = new NguoiChoiDauGiaDAO(WebConfiguration.DbConnectionString);
            var dsNguoiDauGia = rpt2.SelectByIDPhienDauGia(dto.ID);

            if (dsNguoiDauGia != null && dsNguoiDauGia.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow datarow in dsNguoiDauGia.Tables[0].Rows)
                {
                    // Add tat ca nguoi choi co tham gia phien dau gia vao list
                    dto.NguoiChoiDauGia.Add(NguoiChoiDauGiaDTO.Translate(datarow));
                }
            }

			return dto;
		}
	}
}